
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

import 'FullScreen.dart';
import 'LoadView.dart';

class VideoPlayerPlus extends StatefulWidget {
  final VideoPlayerController controller;

  const VideoPlayerPlus(
    this.controller, {
    Key key,
  }) : super(key: key);

  @override
  _VideoPlayerPlusState createState() => _VideoPlayerPlusState();
}

class _VideoPlayerPlusState extends State<VideoPlayerPlus> {
  VideoPlayerController _controller;
  bool _play = false;
  bool _volume = true;
  var initialize;
  Function _function;

  @override
  void initState() {
    super.initState();
    _controller = widget.controller;
    _function = () {
      var duration = _controller.value.duration;
      var position = _controller.value.position;
      if (position.inMilliseconds > 0 && duration.compareTo(position) == 0) {
        setState(() {
          _play = false;
        });
      }
    };
    _controller.addListener(_function);
    initialize = _controller.initialize();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(maxHeight: 210, minHeight: 200),
      color: Colors.black,
      child: FutureBuilder(
          future: initialize,
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            //请求完成
            if (snapshot.connectionState == ConnectionState.done) {
              //发生错误
              if (snapshot.hasError) {
                debugPrint(snapshot.error.toString());
                _controller.dispose();
                return Center(
                    child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    IconButton(
                        icon: Icon(
                          Icons.error_outline,
                          color: Colors.white,
                        ),
                        onPressed: () {
                          setState(() {
                            initialize = _controller.initialize();
                          });
                        }),
                    Text(
                      "视频加载失败",
                      style: TextStyle(color: Colors.white),
                    )
                  ],
                ));
              }
              return Stack(
                children: <Widget>[
                  Center(
                      child: Container(
                    width: geWidth(_controller.value),
                    height: 200,
                    child: GestureDetector(
                      child: VideoPlayer(_controller),
                      onTap: () {
                        _play = false;
                        _controller.pause();
                        setState(() {});
                      },
                    ),
                  )),
                  Visibility(
                    child: Center(
                      child: IconButton(
                          icon: Icon(
                            Icons.play_circle_outline,
                            size: 40,
                            color: Colors.white,
                          ),
                          onPressed: () {
                            _play = true;
                            var duration = _controller.value.duration;
                            var position = _controller.value.position;
                            if (position.inMilliseconds > 0 &&
                                duration.compareTo(position) == 0) {
                              setState(() {
                                _controller.seekTo(Duration());
                              });
                              return;
                            }
                            _controller.play();
                            setState(() {});
                          }),
                    ),
                    visible: !_play,
                  ),
                  Positioned(
                    child: IconButton(
                        icon: Icon(
                          Icons.fullscreen,
                          color: Colors.white,
                        ),
                        onPressed: () {
                          _controller.pause();
                          Navigator.push(context,
                              MaterialPageRoute(builder: (context) {
                            return FullScreen(
                              controller: _controller,
                              play: _play == true,
                            );
                          }));
                        }),
                    bottom: 5,
                    right: 5,
                  ),
                  Positioned(
                    child: IconButton(
                        icon: Icon(
                          _volume ? Icons.volume_up : Icons.volume_off,
                          color: Colors.white,
                        ),
                        onPressed: () {
                          setState(() {
                            if (_volume)
                              _controller.setVolume(0);
                            else
                              _controller.setVolume(1);
                            _volume = !_volume;
                          });
                        }),
                    bottom: 5,
                    left: 5,
                  )
                ],
              );
            }
            return LoadView();
          }),
    );
  }

  geWidth(VideoPlayerValue value) {
    var aspectRatio = value.aspectRatio;
    return 200 * aspectRatio;
  }

  @override
  void dispose() {
    super.dispose();
    if (_controller.value.isPlaying) {
      _controller.pause();
    }
    _controller.removeListener(_function);
    _controller.dispose();
  }
}
